﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    public class OptionsProvider : ObjectBase
    {
        #region API 函数
        /// <summary>
        /// 获取选项设置器的名字
        /// </summary>
        /// <param name="optionProvider"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static protected extern SUResult SUOptionsProviderGetName(IntPtr optionProvider, ref IntPtr name);

        /// <summary>
        /// 获取选项个数
        /// </summary>
        /// <param name="optionProvider"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static protected extern SUResult SUOptionsProviderGetNumKeys(IntPtr optionProvider, out long count);

        /// <summary>
        /// 获取全部选项名字
        /// </summary>
        /// <param name="optionProvider"></param>
        /// <param name="len"></param>
        /// <param name="keys"></param>
        /// <param name="returnCount"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static protected extern SUResult SUOptionsProviderGetKeys(IntPtr optionProvider, long len, IntPtr[] keys, out long returnCount);

        /// <summary>
        /// 获取选项对应的值
        /// </summary>
        /// <param name="optionProvider"></param>
        /// <param name="name">选项名字</param>
        /// <param name="value"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static protected extern SUResult SUOptionsProviderGetValue(IntPtr optionProvider, [MarshalAs(UnmanagedType.LPUTF8Str)] string name, out IntPtr value);

        /// <summary>
        /// 设置选项的值
        /// </summary>
        /// <param name="optionProvider"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static protected extern SUResult SUOptionsProviderSetValue(IntPtr optionProvider, [MarshalAs(UnmanagedType.LPUTF8Str)] string name, IntPtr value);
        #endregion

        /// <summary>
        /// 选项设置器的名字
        /// </summary>
        public string Name
        {
            get
            {
                StringRef ss = StringRef.Empty;
                var res = SUOptionsProviderGetName(Id, ref ss.Id);
                return ss.Dispose();
            }
        }

        /// <summary>
        /// 选项个数
        /// </summary>
        public int Count
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUOptionsProviderGetNumKeys(Id, out long count));
                return (int)count;
            }
        }

        /// <summary>
        /// 所有选项的名字
        /// </summary>
        public List<string> OptionNames
        {
            get
            {
                StringRef[] ss = new StringRef[Count];
                for (int i = 0; i < ss.Length; i++)
                {
                    ss[i] = StringRef.Empty;
                }
                IntPtr[] ptrs = ss.Select(s => s.Id).ToArray();
                SketchUpExceptionHelper.ThrowSUResult(SUOptionsProviderGetKeys(Id, Count, ptrs, out long returnCount));

                return ss.Select(s => s.Dispose()).ToList();
            }
        }

        /// <summary>
        /// 获取设置选项的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public TypedValue this[string key]
        {
            get
            {
                TypedValue value = new TypedValue();
                SketchUpExceptionHelper.ThrowSUResult(SUOptionsProviderGetValue(Id, key, out value.Id));
                return value;
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUOptionsProviderSetValue(Id, key, value.Id));
            }
        }

        internal OptionsProvider()
        {

        }
    }
}